package com.zyx.yygh.hosp.controller.api;

import com.zyx.yygh.common.exception.YyghException;
import com.zyx.yygh.common.helper.HttpRequestHelper;
import com.zyx.yygh.common.result.Result;
import com.zyx.yygh.common.result.ResultCodeEnum;
import com.zyx.yygh.common.util.MD5;
import com.zyx.yygh.hosp.service.DepartmentService;
import com.zyx.yygh.hosp.service.HospitalService;
import com.zyx.yygh.hosp.service.HospitalSetService;
import com.zyx.yygh.hosp.service.ScheduleService;
import com.zyx.yygh.model.hosp.Department;
import com.zyx.yygh.model.hosp.Hospital;
import com.zyx.yygh.model.hosp.Schedule;
import com.zyx.yygh.vo.hosp.DepartmentQueryVo;
import com.zyx.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author zyx
 * @version 1.0
 * @date 2022/1/20 020 18:00
 */

@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Resource
    private HospitalService hospitalService;

    @Resource
    private HospitalSetService hospitalSetService;

    @Resource
    private DepartmentService departmentService;

    @Resource
    private ScheduleService scheduleService;

    /**
     * 删除排班
     */
    @PostMapping("schedule/remove")
    public Result remove(HttpServletRequest request){

        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");

        //签名校验
        String hospSign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);

        String encrypt = MD5.encrypt(signKey);

        if (!hospSign.equals(encrypt)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }

    /**
     * 查询排班
     */
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){

        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");

        int page = StringUtils.isEmpty(paramMap.get("page")) ?
                1 : Integer.parseInt((String) paramMap.get("page"));

        int limit = StringUtils.isEmpty(paramMap.get("limit")) ?
                1 : Integer.parseInt((String) paramMap.get("limit"));
        //签名校验
        String hospSign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);

        String encrypt = MD5.encrypt(signKey);

        if (!hospSign.equals(encrypt)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        Page<Schedule> pageModel = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 上传排班
     */
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){

        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //签名校验
        String hoscode = (String) paramMap.get("hoscode");
        String hospSign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);

        String encrypt = MD5.encrypt(signKey);

        if (!hospSign.equals(encrypt)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.save(paramMap);
        return Result.ok();
    }

    /**
     * 删除科室
     */
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){

        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");

        //签名校验
        String hospSign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);

        String encrypt = MD5.encrypt(signKey);

        if (!hospSign.equals(encrypt)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.remove(hoscode,depcode);

        return Result.ok();
    }

    /**
     * 查询科室接口
     */
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) {

        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        String hoscode = (String) paramMap.get("hoscode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ?
                1 : Integer.parseInt((String) paramMap.get("page"));

        int limit = StringUtils.isEmpty(paramMap.get("limit")) ?
                1 : Integer.parseInt((String) paramMap.get("limit"));
        //签名校验
        String hospSign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);

        String encrypt = MD5.encrypt(signKey);

        if (!hospSign.equals(encrypt)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        Page<Department> pageModel = departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageModel);

    }

    /**
     * 上传科室接口
     */
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {

        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        String hoscode = (String) paramMap.get("hoscode");
        String hospSign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);

        String encrypt = MD5.encrypt(signKey);

        if (!hospSign.equals(encrypt)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.save(paramMap);
        return Result.ok();
    }

    /**
     * 查询医院
     */
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {

        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        String hoscode = (String) paramMap.get("hoscode");
        String hospSign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);

        String encrypt = MD5.encrypt(signKey);

        if (!hospSign.equals(encrypt)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    /**
     * 上传医院接口
     */
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) {

        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        String hospSign = (String) paramMap.get("sign");
        String hoscode = (String) paramMap.get("hoscode");

        String signKey = hospitalSetService.getSignKey(hoscode);
        String encrypt = MD5.encrypt(signKey);

        if (!hospSign.equals(encrypt)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);

        hospitalService.save(paramMap);
        return Result.ok();
    }

}
